home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / gdevpdff.h < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  13.8 KB  |  361 lines

  1. /* Copyright (C) 1999, 2000 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: gdevpdff.h,v 1.4 2000/09/19 19:00:17 lpd Exp $ */
  20. /* Font-related definitions for PDF-writing driver. */
  21.  
  22. #ifndef gdevpdff_INCLUDED
  23. #  define gdevpdff_INCLUDED
  24.  
  25. /* ================ Types and structures ================ */
  26.  
  27. /*
  28.  * The PDF writer creates several different kinds of font resources.
  29.  * The key differences between them are the values of num_chars, index,
  30.  * and descriptor.
  31.  *
  32.  *    - Synthesized Type 3 bitmap fonts are identified by num_chars != 0 (or
  33.  *    equivalently PDF_FONT_IS_SYNTHESIZED = true).  They have index < 0,
  34.  *    FontDescriptor == 0.  All other fonts have num_chars == 0 and
  35.  *    FontDescriptor != 0.
  36.  *
  37.  *    - The base 14 fonts (when not embedded) have num_chars == 0, index
  38.  *    >= 0, FontDescriptor != 0, FontDescriptor->base_font == 0.  All
  39.  *    other fonts have index < 0.
  40.  *
  41.  *    - All other fonts, embedded or not, have num_chars == 0, index < 0,
  42.  *    FontDescriptor != 0, FontDescriptor->base_font != 0.  A font is
  43.  *    embedded iff FontDescriptor->FontFile_id != 0.
  44.  *
  45.  * For non-synthesized fonts, the structure representation is designed to
  46.  * represent directly the information that will be written in the font
  47.  * resource, Encoding, and FontDescriptor dictionaries.  See the comments
  48.  * on the pdf_font_t structure below for more detail.
  49.  */
  50.  
  51. /*
  52.  * PDF font names must be large enough for the 14 built-in fonts,
  53.  * and also large enough for any reasonable font name + 7 characters
  54.  * for the subsetting prefix + a suffix derived from the PDF object number.
  55.  */
  56. #define SUBSET_PREFIX_SIZE 7
  57. #define MAX_PDF_FONT_NAME\
  58.   (SUBSET_PREFIX_SIZE + gs_font_name_max + 1 + 1 + sizeof(long) * 2)
  59. typedef struct pdf_font_name_s {
  60.     byte chars[MAX_PDF_FONT_NAME];
  61.     uint size;
  62. } pdf_font_name_t;
  63.  
  64. /* ---------------- Font descriptor (pseudo-resource) ---------------- */
  65.  
  66. /*
  67.  * A FontDescriptor refers to an unscaled, possibly built-in base_font.
  68.  * Multiple pdf_fonts with the same outlines (but possibly different
  69.  * encodings, metrics, and scaling) may share a single FontDescriptor.
  70.  * Each such pdf_font refers to a corresponding gs_font object, and
  71.  * the gs_fonts of all such pdf_fonts will have the base_font of the
  72.  * FontDescriptor as their eventual base font (through a chain of 0 or
  73.  * more base pointers).
  74.  *
  75.  * Since gs_font objects may be freed at any time, we register a procedure
  76.  * to be called when that happens.  The (gs_)font of a pdf_font may be freed
  77.  * either before or after the base_font in the FontDescriptor.  Therefore, a
  78.  * pdf_font copies enough information from its gs_font that when the gs_font
  79.  * is freed, the pdf_font still has enough information to write the Font
  80.  * resource dictionary at a later time.  When freeing a gs_font referenced
  81.  * from a pdf_font, we only clear the pointer to it from its referencing
  82.  * pdf_font.  However, when the base_font of a FontDescriptor is about to be
  83.  * freed, we must write the FontDescriptor, and, if the font is embedded,
  84.  * the FontFile data.
  85.  */
  86.  
  87. /*
  88.  * Font descriptors are handled as pseudo-resources.  Multiple pdf_fonts
  89.  * may share a descriptor.  We don't need to use reference counting to
  90.  * keep track of this, since all descriptors persist until the device
  91.  * is closed, even though the base_font they reference may have been
  92.  * freed.
  93.  */
  94. typedef struct pdf_font_descriptor_values_s {
  95.     font_type FontType;        /* copied from font */
  96.     /* Required elements */
  97.     int Ascent, CapHeight, Descent, ItalicAngle, StemV;
  98.     gs_int_rect FontBBox;
  99.     uint Flags;
  100.     /* Optional elements (default to 0) */
  101.     int AvgWidth, Leading, MaxWidth, MissingWidth, StemH, XHeight;
  102. } pdf_font_descriptor_values_t;
  103.  
  104. #ifndef pdf_font_descriptor_DEFINED
  105. #  define pdf_font_descriptor_DEFINED
  106. typedef struct pdf_font_descriptor_s pdf_font_descriptor_t;
  107. #endif
  108.  
  109. struct pdf_font_descriptor_s {
  110.     pdf_resource_common(pdf_font_descriptor_t);
  111.     pdf_font_name_t FontName;
  112.     pdf_font_descriptor_values_t values;
  113.     gs_matrix orig_matrix;    /* unscaled font matrix */
  114.     gs_string chars_used;    /* 1 bit per character code or CID */
  115.     gs_string glyphs_used;    /* 1 bit per glyph, for TrueType fonts */
  116.     bool subset_ok;        /* if false, don't subset the font -- */
  117.                 /* see gdevpdft.c */
  118.     long FontFile_id;        /* non-0 iff the font is embedded */
  119.     gs_font *base_font;        /* unscaled font defining the base encoding, */
  120.                 /* matrix, and character set, 0 iff */
  121.                 /* non-embedded standard font */
  122.     bool notified;        /* if true, base_font will notify on freeing */
  123.     bool written;        /* if true, descriptor has been written out */
  124. };
  125. /* Flag bits */
  126. /*#define FONT_IS_FIXED_WIDTH (1<<0)*/  /* defined in gxfont.h */
  127. #define FONT_IS_SERIF (1<<1)
  128. #define FONT_IS_SYMBOLIC (1<<2)
  129. #define FONT_IS_SCRIPT (1<<3)
  130. /*
  131.  * There is confusion over the meaning of the 1<<5 bit.  According to the
  132.  * published PDF documentation, in PDF 1.1, it meant "font uses
  133.  * StandardEncoding", and as of PDF 1.2, it means "font uses (a subset of)
  134.  * the Adobe standard Latin character set"; however, Acrobat Reader 3 and 4
  135.  * seem to use the former interpretation, even if the font is embedded and
  136.  * the file is identified as a PDF 1.2 file.  We have to use the former
  137.  * interpretation in order to produce output that Acrobat will handle
  138.  * correctly.
  139.  */
  140. #define FONT_USES_STANDARD_ENCODING (1<<5) /* always used */
  141. #define FONT_IS_ADOBE_ROMAN (1<<5) /* never used */
  142. #define FONT_IS_ITALIC (1<<6)
  143. #define FONT_IS_ALL_CAPS (1<<16)
  144. #define FONT_IS_SMALL_CAPS (1<<17)
  145. #define FONT_IS_FORCE_BOLD (1<<18)
  146. /*
  147.  * Font descriptors are pseudo-resources, so their GC descriptors
  148.  * must be public.
  149.  */
  150. #define public_st_pdf_font_descriptor()    /* in gdevpdff.c */\
  151.   BASIC_PTRS(pdf_font_descriptor_ptrs) {\
  152.     GC_STRING_ELT(pdf_font_descriptor_t, chars_used),\
  153.     GC_STRING_ELT(pdf_font_descriptor_t, glyphs_used),\
  154.     GC_OBJ_ELT(pdf_font_descriptor_t, base_font)\
  155.   };\
  156.   gs_public_st_basic_super(st_pdf_font_descriptor, pdf_font_descriptor_t,\
  157.     "pdf_font_descriptor_t", pdf_font_descriptor_ptrs,\
  158.     pdf_font_descriptor_data, &st_pdf_resource, 0)
  159.  
  160. /* ---------------- Font (resource) ---------------- */
  161.  
  162. typedef struct pdf_char_proc_s pdf_char_proc_t;    /* forward reference */
  163. /*typedef struct pdf_font_s pdf_font_t;*/
  164. typedef struct pdf_encoding_element_s {
  165.     gs_glyph glyph;
  166.     gs_const_string str;
  167. } pdf_encoding_element_t;
  168. #define private_st_pdf_encoding_element()\
  169.   gs_private_st_composite(st_pdf_encoding_element, pdf_encoding_element_t,\
  170.     "pdf_encoding_element_t[]", pdf_encoding_elt_enum_ptrs,\
  171.     pdf_encoding_elt_reloc_ptrs)
  172. /*
  173.  * Structure elements beginning with a capital letter correspond directly
  174.  * to keys in the font or Encoding dictionary.  Currently these are:
  175.  *    (font) FontType, FirstChar, LastChar, Widths, FontDescriptor
  176.  *    (Encoding) BaseEncoding, Differences
  177.  */
  178. struct pdf_font_s {
  179.     pdf_resource_common(pdf_font_t);
  180.     pdf_font_name_t fname;
  181.     font_type FontType;
  182.     gs_font *font;        /* non-0 iff font will notify us; */
  183.                 /* should be a weak pointer */
  184.     int index;            /* in pdf_standard_fonts, -1 if not base 14 */
  185.     gs_matrix orig_matrix;    /* FontMatrix of unscaled font for embedding */
  186.     bool is_MM_instance;    /* for Type 1/2 fonts, true iff the font */
  187.                 /* is a Multiple Master instance */
  188.     /*
  189.      * For synthesized fonts, frname is A, B, ...; for other fonts,
  190.      * frname is R<id>.  The string is null-terminated.
  191.      */
  192.     char frname[1/*R*/ + (sizeof(long) * 8 / 3 + 1) + 1/*\0*/];
  193.     /* Encoding for base fonts. */
  194.     gs_encoding_index_t BaseEncoding;
  195.     pdf_encoding_element_t *Differences; /* [256] */
  196.     /* Bookkeeping for non-synthesized fonts. */
  197.     pdf_font_descriptor_t *FontDescriptor;
  198.     int FirstChar, LastChar;
  199.     bool write_Widths;
  200.     int Widths[256];
  201.     byte widths_known[32];    /* 1 bit per character code */
  202.     bool skip;            /* font was already written, skip it */
  203.     /* Bookkeeping for synthesized fonts. */
  204.     int num_chars;
  205. #define PDF_FONT_IS_SYNTHESIZED(pdfont) ((pdfont)->num_chars != 0)
  206.     pdf_char_proc_t *char_procs;
  207.     int max_y_offset;
  208.     /* Pseudo-characters for spacing. */
  209.     /* The range should be determined by the device resolution.... */
  210. /*#define X_SPACE_MIN xxx*/ /* in gdevpdfx.h */
  211. /*#define X_SPACE_MAX nnn*/ /* in gdevpdfx.h */
  212.     byte spaces[X_SPACE_MAX - X_SPACE_MIN + 1];
  213. };
  214.  
  215. /* The descriptor is public for pdf_resource_type_structs. */
  216. #define public_st_pdf_font()\
  217.   gs_public_st_suffix_add4(st_pdf_font, pdf_font_t, "pdf_font_t",\
  218.     pdf_font_enum_ptrs, pdf_font_reloc_ptrs, st_pdf_resource,\
  219.     font, Differences, FontDescriptor, char_procs)
  220.  
  221. /* CharProc pseudo-resources for synthesized fonts */
  222. struct pdf_char_proc_s {
  223.     pdf_resource_common(pdf_char_proc_t);
  224.     pdf_font_t *font;
  225.     pdf_char_proc_t *char_next;    /* next char_proc for same font */
  226.     int width, height;
  227.     int x_width;        /* X escapement */
  228.     int y_offset;        /* of character (0,0) */
  229.     byte char_code;
  230. };
  231.  
  232. /* The descriptor is public for pdf_resource_type_structs. */
  233. #define public_st_pdf_char_proc()\
  234.   gs_public_st_suffix_add2(st_pdf_char_proc, pdf_char_proc_t,\
  235.     "pdf_char_proc_t", pdf_char_proc_enum_ptrs,\
  236.     pdf_char_proc_reloc_ptrs, st_pdf_resource, font, char_next)
  237.  
  238. /* ================ Procedures ================ */
  239.  
  240. /* ---------------- Exported by gdevpdft.c ---------------- */
  241.  
  242. /* Begin a CharProc for an embedded (bitmap) font. */
  243. int pdf_begin_char_proc(P8(gx_device_pdf * pdev, int w, int h, int x_width,
  244.                int y_offset, gs_id id, pdf_char_proc_t **ppcp,
  245.                pdf_stream_position_t * ppos));
  246.  
  247. /* End a CharProc. */
  248. int pdf_end_char_proc(P2(gx_device_pdf * pdev, pdf_stream_position_t * ppos));
  249.  
  250. /* Put out a reference to an image as a character in an embedded font. */
  251. int pdf_do_char_image(P3(gx_device_pdf * pdev, const pdf_char_proc_t * pcp,
  252.              const gs_matrix * pimat));
  253.  
  254. /* ---------------- Exported by gdevpdff.c ---------------- */
  255.  
  256. typedef enum {
  257.     FONT_EMBED_STANDARD,    /* 14 standard fonts */
  258.     FONT_EMBED_NO,
  259.     FONT_EMBED_YES
  260. } pdf_font_embed_t;
  261.  
  262. typedef struct pdf_standard_font_s {
  263.     const char *fname;
  264.     gs_encoding_index_t base_encoding;
  265. } pdf_standard_font_t;
  266. extern const pdf_standard_font_t pdf_standard_fonts[];
  267.  
  268. /* Return the index of a standard font name, or -1 if missing. */
  269. int pdf_find_standard_font(P2(const byte *str, uint size));
  270.  
  271. /*
  272.  * Find the original (unscaled) standard font corresponding to an
  273.  * arbitrary font, if any.  Return its index in standard_fonts, or -1.
  274.  */
  275. int pdf_find_orig_font(P3(gx_device_pdf *pdev, gs_font *font,
  276.               gs_matrix *pfmat));
  277.  
  278. /*
  279.  * Determine the embedding status of a font.  If the font is in the base
  280.  * 14, store its index (0..13) in *pindex and its similarity to the base
  281.  * font (as determined by the font's same_font procedure) in *psame.
  282.  */
  283. pdf_font_embed_t pdf_font_embed_status(P4(gx_device_pdf *pdev, gs_font *font,
  284.                       int *pindex, int *psame));
  285.  
  286. /*
  287.  * Allocate a font resource.  If pfd != 0, a FontDescriptor is allocated,
  288.  * with its id, values, and chars_used.size taken from *pfd.
  289.  */
  290. int pdf_alloc_font(P4(gx_device_pdf *pdev, gs_id rid, pdf_font_t **ppfres,
  291.               const pdf_font_descriptor_t *pfd));
  292.  
  293. /*
  294.  * Determine whether a font is a subset font by examining the name.
  295.  */
  296. bool pdf_has_subset_prefix(P2(const byte *str, uint size));
  297.  
  298. /*
  299.  * Make the prefix for a subset font from the font's resource ID.
  300.  */
  301. void pdf_make_subset_prefix(P2(byte *str, ulong id));
  302.  
  303. /*
  304.  * Adjust the FontName of a newly created FontDescriptor so that it is
  305.  * unique if necessary.  If the name was changed, return 1.
  306.  */
  307. int pdf_adjust_font_name(P3(const gx_device_pdf *pdev,
  308.                 pdf_font_descriptor_t *pfd,
  309.                 bool is_standard));
  310.  
  311. /* Add an encoding difference to a font. */
  312. int pdf_add_encoding_difference(P5(gx_device_pdf *pdev, pdf_font_t *ppf,
  313.                    int chr, const gs_font_base *bfont,
  314.                    gs_glyph glyph));
  315.  
  316. /* Get the width of a given character in a (base) font. */
  317. int pdf_char_width(P4(pdf_font_t *ppf, int ch, gs_font *font,
  318.               int *pwidth /* may be NULL */));
  319.  
  320. /*
  321.  * Find the range of character codes that includes all the defined
  322.  * characters in a font.
  323.  */
  324. void pdf_find_char_range(P3(gs_font *font, int *pfirst, int *plast));
  325.  
  326. /* Compute the FontDescriptor for a font or a font subset. */
  327. int pdf_compute_font_descriptor(P4(gx_device_pdf *pdev,
  328.                    pdf_font_descriptor_t *pfd, gs_font *font,
  329.                    const byte *used /*[32]*/));
  330.  
  331. /* Unregister the standard fonts when cleaning up. */
  332. void pdf_unregister_fonts(P1(gx_device_pdf *pdev));
  333.  
  334. /* ---------------- Exported by gdevpdfw.c ---------------- */
  335.  
  336. /* Register a font for eventual writing (embedded or not). */
  337. int pdf_register_font(P3(gx_device_pdf *pdev, gs_font *font, pdf_font_t *ppf));
  338.  
  339. /* Write out the font resources when wrapping up the output. */
  340. int pdf_write_font_resources(P1(gx_device_pdf *pdev));
  341.  
  342. /*
  343.  * Write a font descriptor.
  344.  * (Exported only for gdevpdfe.c.)
  345.  */
  346. int pdf_write_FontDescriptor(P2(gx_device_pdf *pdev,
  347.                 const pdf_font_descriptor_t *pfd));
  348.  
  349.  
  350. /* ---------------- Exported by gdevpdfe.c ---------------- */
  351.  
  352. /*
  353.  * Write the FontDescriptor and FontFile* data for an embedded font.
  354.  * Return a rangecheck error if the font can't be embedded.
  355.  * (Exported only for gdevpdfw.c.)
  356.  */
  357. int pdf_write_embedded_font(P2(gx_device_pdf *pdev,
  358.                    pdf_font_descriptor_t *pfd));
  359.  
  360. #endif /* gdevpdff_INCLUDED */
  361.